#ifndef __APP_SENSOR_PROCESS_H__
#define __APP_SENSOR_PROCESS_H__

#include "app_communication.h"

#define SENSOR_PACKET_FRAME_SIZE 5U  /* 一包数据5帧 */
#define SENSOR_MSG_FRAME_SIZE    8U  /* 一帧传感器数据为8Byte */
#define SENSOR_PRESS_VALUE_COUNT 14U /* 传感器压力值数量 */

/**
 * @brief 传感器类型定义
 *
 */
typedef enum
{
    /* LK1A */
    SENSOR_TYPE_LK1A_LEG    = 3U + SENSOR_BASE_ID,
    SENSOR_TYPE_LK1A_LUMBAR = 4U + SENSOR_BASE_ID,
    SENSOR_TYPE_LK1A_RIGHT  = 6U + SENSOR_BASE_ID,
    SENSOR_TYPE_LK1A_LEFT   = 7U + SENSOR_BASE_ID,

    /* PK1B */
    SENSOR_TYPE_PK1B_BACK   = 1U + SENSOR_BASE_ID,
    SENSOR_TYPE_PK1B_LEG    = 2U + SENSOR_BASE_ID,
    SENSOR_TYPE_PK1B_LUMBAR = 5U + SENSOR_BASE_ID,
    SENSOR_TYPE_PK1B_RIGHT  = 8U + SENSOR_BASE_ID,
    SENSOR_TYPE_PK1B_LEFT   = 9U + SENSOR_BASE_ID,

    SENSOR_TYPE_MAX = 9U,
} sensor_type_e;

/**
 * @brief 传感器数据包状态
 *
 */
typedef enum
{
    PACKET_STATE_IDLE      = 0U, /*!< 空闲状态 */
    PACKET_STATE_RECEIVING = 1U, /*!< 正在接收 */
    PACKET_STATE_COMPLETE  = 2U, /*!< 接收完成 */
    PACKET_STATE_ERROR     = 3U, /*!< 接收错误 */
} packet_state_e;

/**
 * @brief 传感器数据, 5帧数据
 *
 */
typedef struct
{
    uint8_t  data[SENSOR_PACKET_FRAME_SIZE * SENSOR_MSG_FRAME_SIZE]; /*!< 传感器数据, 5帧数据, 每帧8字节 */
    bool     is_ready;                                               /*!< 数据是否就绪 */
    uint32_t timestamp;                                              /*!< 时间戳 */
} sensor_data_t;

/**
 * @brief 传感器数据包, 单个传感器
 *
 */
typedef struct
{
    uint8_t data[SENSOR_PACKET_FRAME_SIZE][SENSOR_MSG_FRAME_SIZE]; /*!< 传感器数据, 5帧数据, 每帧8字节 */
    uint8_t frame_count;                                           /*!< 已接收帧数 */
    uint8_t expected_seq;                                          /*!< 期望的下一帧序号（0x20-0x24） */
    packet_state_e state;                                          /*!< 数据包状态 */
    uint32_t       timestamp;                                      /*!< 时间戳 */
} sensor_packet_t;

/**
 * @brief 传感器管理器
 *
 */
typedef struct
{
    sensor_packet_t packets[SENSOR_TYPE_MAX];          /*!< 每种传感器的数据包  */
    sensor_data_t   sensor_data_list[SENSOR_TYPE_MAX]; /*!< 传感器数据列表 */
    uint16_t sensor_press_values_list[SENSOR_TYPE_MAX][SENSOR_PRESS_VALUE_COUNT]; /*!< 传感器压力值列表 */
} sensor_manager_t;

extern sensor_manager_t sensor_manager;

void app_sensor_process_handle(void);

#endif
